//
//  CYCarUnionVC.m
//  CreditAddressBookEE
//
//  Created by LE on 16/2/16.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYCarUnionVC.h"
#import "CYRewardInfoListTVC.h"
#import "LEPageViewController.h"
#import "CYReceiveRewardListTVC.h"
#import "CYDiscoveryHttpTool.h"
#import "MBProgressHUD+LE.h"
#import "CYSimpleLoanRewardM.h"
#import "CYCompanyUserTool.h"
#import "CYJumpsTool.h"
#import "PaperButton.h"
#import "LEMoreView.h"
#import "CYCarLoanPublishTVC.h"
#import "CYMyCarLoanVC.h"
#import "CYMyApplyRewardVC.h"
#import "CYLoginHttpTool.h"
#import "CYHelpCarVC.h"
#import "DSNavigationBar.h"

@interface CYCarUnionVC ()<UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UIView *topBgView;
@property (weak, nonatomic) IBOutlet UILabel *carLabel;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UIView *dataView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;
@property (weak, nonatomic) IBOutlet UILabel *companycount;
@property (weak, nonatomic) IBOutlet UIView *backview;

@property(nonatomic, assign) LEPageViewController *pageViewController;
@property(nonatomic, weak) CYRewardInfoListTVC *rewardInfoList;
@property(nonatomic, weak) CYReceiveRewardListTVC *receiveRewardList;
@property(nonatomic, strong) NSArray *simpleList;//最新车辆缓存

@end

@implementation CYCarUnionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _backview.backgroundColor = CYColor(113, 211, 119, 1.0);
    self.title = @"车贷联盟";
    [self initContent];
    [self addBarButton];
    [self loadData];
}
/*!
 *  @brief  初始设置
 */
- (void)initContent {
    self.searchBar.layer.borderColor = CYNaviThemeColor.CGColor;
    self.searchBar.layer.borderWidth = 1;
    self.topBgView.layer.borderColor = CYNaviThemeColor.CGColor;
    self.topBgView.layer.borderWidth = 1;
    
    CYRewardInfoListTVC *infoList = [[CYRewardInfoListTVC alloc] init];
    CYReceiveRewardListTVC *infoList1 = [[CYReceiveRewardListTVC alloc] init];
    self.rewardInfoList = infoList;
    self.receiveRewardList = infoList1;
    CYWeakObject(wself, self);
    LEPageViewController *page = [[LEPageViewController alloc] initWithControllers:@[infoList, infoList1] controllerChanged:^(NSInteger selectedIndex) {
        wself.segment.selectedSegmentIndex = selectedIndex;
        [wself.searchBar resignFirstResponder];
    }];
    self.pageViewController = page;
    [self addChildViewController:page];
    [self.dataView addSubview:page.view];
    [page didMoveToParentViewController:self];
    [page.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
    
    self.searchBar.delegate = self;
    self.searchBar.showsCancelButton = NO;
}
/*!
 *  @brief  从服务器加载数据
 */
- (void)loadData {
    [MBProgressHUD showMessage:nil toView:self.view];
    [CYDiscoveryHttpTool simpleLoanListWithParam:nil success:^(CYSimpleLoanRewardM *reward, NSArray *simpleList, NSArray *rewardList) {
        [MBProgressHUD hideHUDForView:self.view];
        self.rewardInfoList.list = simpleList;
        self.rewardInfoList.type = CYRewardTypeDefault;
        self.simpleList = simpleList;
        self.receiveRewardList.list = rewardList;
//        self.carLabel.text = reward.rewardCarNumber;
        self.carLabel.text = [NSString stringWithFormat:@"赏金车辆%@辆",reward.rewardCarNumber];
//        self.moneyLabel.text = reward.rewardCarMoney;
        float intstr = [reward.rewardCarMoney intValue];
        self.moneyLabel.text = [NSString stringWithFormat:@"%.2f",intstr/10000];

//        self.companycount.text = reward.allCompany;
        self.companycount.text = [NSString stringWithFormat:@"收录公司%@家",reward.allCompany];
    } failure:^(NSString *message, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:NO];
        [MBProgressHUD showError:message];
    }];
    
    
}
/*!
 *  @brief  搜索
 */
- (void)search:(NSString *)searchKey {
    CYUser *user = [CYLoginHttpTool userData].user;
    if (user) {
        if (searchKey && !searchKey.isEmpty) {
            [MBProgressHUD showMessage:nil toView:self.view];
            [CYDiscoveryHttpTool getLoanSearchWithParam:@{@"userId": user.userId, @"token": user.token, @"condition": searchKey, @"type": @"0"} success:^(NSArray *simpleList) {
                [MBProgressHUD hideHUDForView:self.view];
                self.rewardInfoList.list = simpleList;
                self.rewardInfoList.type = CYRewardTypeSearch;
                [self.searchBar resignFirstResponder];
            } failure:^(NSString *message, BOOL isLogin) {
                [MBProgressHUD hideHUDForView:self.view animated:NO];
                [MBProgressHUD showError:message];
                [self.searchBar resignFirstResponder];
                self.searchBar.text = nil;
                if (!isLogin) {
                    [CYCompanyUserTool logoutWithController:self];
                }
            }];
        } else {
            [self.searchBar resignFirstResponder];
        }
    } else {
        [CYJumpsTool jumpToCompanyLogin:self];
        [self.searchBar resignFirstResponder];
        self.searchBar.text = nil;
    }
}

- (IBAction)segment:(UISegmentedControl *)sender {
    self.pageViewController.selectedIndex = sender.selectedSegmentIndex;
    [self.searchBar resignFirstResponder];
}
/*!
 *  @brief  添加跟多button
 */
- (void)addBarButton {
    PaperButton *more = [PaperButton button];
    [more addTarget:self action:@selector(paperButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    more.tintColor = [UIColor whiteColor];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:more];
    self.navigationItem.rightBarButtonItem = barButton;
}
/*!
 *  @brief  更多点击
 */
- (void)paperButtonClick:(PaperButton *)btn {
    CYWeakObject(wself, self);

        [LEMoreView showMenuWithData:@[@"我申请的悬赏", @"帮助"] icons:@[@"US Dollar-48", @"About-48"] showInView:self.navigationController.view didSelectRow:^(NSInteger indexPath) {
             if (indexPath == 0) {
                CYMyApplyRewardVC *carLoan = [[CYMyApplyRewardVC alloc] init];
                [wself.navigationController pushViewController:carLoan animated:YES];
            } else if (indexPath == 1) {
                CYHelpCarVC *help = [[CYHelpCarVC alloc] init];
                [self.navigationController pushViewController:help animated:YES];
                
            }
            [btn buttonTouched];
        }];
   }

- (void)pub {
    CYCompanyUserModel *user = [CYCompanyUserTool companyUser];
    if (user) {
        CYCarLoanPublishTVC *pub = [CYCarLoanPublishTVC le_loadFromStoryBoard:[CYJumpsTool mainSB]];
        [self.navigationController pushViewController:pub animated:YES];
    } else {
        [CYJumpsTool jumpToCompanyLogin:self];
    }
}

- (void)myPub {
    CYCompanyUserModel *user = [CYCompanyUserTool companyUser];
    if (user) {
        
    } else {
        [CYJumpsTool jumpToCompanyLogin:self];
    }
}

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    if (searchBar.text && !searchBar.text.isEmpty) {
        [self search:searchBar.text];
    }
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    self.searchBar.showsCancelButton = YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    [self.searchBar resignFirstResponder];
    self.searchBar.showsCancelButton = NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    searchBar.text = nil;
    if (self.simpleList) {
        self.rewardInfoList.list = self.simpleList;
        self.rewardInfoList.type = CYRewardTypeDefault;
    } else {
        [self loadData];
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [DSNavigationBar setNavigationBarWithNavigationBar:self.navigationController.navigationBar color:CYNaviThemeColor];
}

- (void)dealloc {
    CYLog(@"车贷联盟销毁");
}
@end
